import { _decorator, Animation, Component, Node, Vec3 } from 'cc';
import { MoveTarget } from './MoveTarget';

const { ccclass, property } = _decorator;

@ccclass('Player')
export class Player extends Component {
    private _target: MoveTarget = null;

    @property(Animation)
    public anim: Animation = null;

    private testData: number = 0.123;

    start() {
        console.log('Player start')

        this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
        const animState = this.anim.getState('jump');
        this._target = new MoveTarget(new Vec3(100, 0, 0), animState.duration, () => {
            this.onMoveEnd();
        });
    }

    onTouchStart(event: TouchEvent) {
        if (this._target.isRunning) {
            return;
        }
        this._target.start();
        this.anim.play('jump');
    }

    update(deltaTime: number) {
        const offset = this._target.update(deltaTime);
        if (offset != 0) {
            this.node.setPosition(this.node.position.add(offset));
        }
    }

    onMoveEnd() {
        console.log('onMoveEnd', this.testData);
    }
}


